package com.wowangz.cms.user.api;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.wowangz.cms.common.pojo.AjaxDataEntity;
import com.wowangz.cms.common.utils.DataFormat;
import com.wowangz.cms.common.utils.SysConstant;
import com.wowangz.cms.component.datagrid.pojo.DataGrid;
import com.wowangz.cms.component.datagrid.pojo.DataResult;
import com.wowangz.cms.component.datagrid.pojo.DataStyle;
import com.wowangz.cms.component.datagrid.pojo.PangingParam;
import com.wowangz.cms.component.datagrid.utils.DataTypeConstant;
import com.wowangz.cms.component.datagrid.utils.PangingTools;
import com.wowangz.cms.syscore.IssTransactionContainer;
import com.wowangz.cms.syscore.api.BaseController;
import com.wowangz.cms.syscore.api.JSONFactory;
import com.wowangz.cms.syscore.utils.MessageResourcesUtils;
import com.wowangz.cms.syscore.utils.ServletUtils;
import com.wowangz.cms.user.pojo.User;
import com.wowangz.cms.user.service.UserService;

/**
 * Controller
 * 
 * @author auto
 */
@Scope("prototype")
@Controller
@SuppressWarnings("unchecked")
public class CheckUserController extends BaseController {
	private static Logger logger = LoggerFactory.getLogger(CheckUserController.class);

	@RequestMapping(value = "/systemmanage/user/checkUser.json", method = { RequestMethod.GET, RequestMethod.POST })
	public void deleteUser(HttpServletRequest request, HttpServletResponse response, Model model) {
		AjaxDataEntity ajaxDataEntity = new AjaxDataEntity();
		try {
			String userId = request.getParameter("id");
			UserService userService = (UserService) IssTransactionContainer.getInstance(UserService.class, false);
			userService.checkUser(userId);
			userContext.setErrorMessage(MessageResourcesUtils.getMessage(request, "alertinfo.common.java.alert8"));
			response.getWriter().write(JSONFactory.toJSONString(ajaxDataEntity, userContext));
		} catch (Exception e) {
			e.printStackTrace();
			userContext.setErrorMessage(MessageResourcesUtils.getMessage(request, "alertinfo.common.java.alert9"));
		}
	}

	@RequestMapping(value = "/systemmanage/user/queryCheckUserList.web")
	public String queryUserList(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "/WEB-INF/jsp/systemmanage/authoritymanage/checkUserList";
	}

	@RequestMapping(value = "/user/queryCheckUserListInfoDataGrid.json", method = RequestMethod.POST)
	public void queryUserListInfoDataGrid(HttpServletRequest request, HttpServletResponse response, Model model) {

		PangingParam pangingParam = null;
		DataResult dataResult = new DataResult();
		DataGrid dataGrid = null;

		try {
			pangingParam = (PangingParam) ServletUtils.getParametersToBaseEntity(request, PangingParam.class);
			HashMap<String, Object> map = new HashMap<String, Object>();
			map = ServletUtils.getParametersToHashMap(request);

			dataGrid = new DataGrid();
			dataGrid.setPangingParam(pangingParam);

			map.put("startrow", dataGrid.getStartrow());
			map.put("endrow", dataGrid.getEndrow());
			map.put("sort", dataGrid.getSort());
			map.put("order", dataGrid.getOrder());
			map.put("statusid", SysConstant.RecordStatus.VALID);
			map.put("checkstatus", SysConstant.CheckStatus.SAVE);
			map.put("loginuserid", userContext.getLoginUser().getId());

			UserService userService = (UserService) IssTransactionContainer.getInstance(UserService.class, false);
			long resultCount = userService.queryCheckUserCount(map);
			List<User> resultList = userService.findResultList(map);

			DataStyle dataStyle = new DataStyle();
			dataStyle.setPageIndex(pangingParam.getPage());
			dataStyle.setDataCount(resultCount);
			dataStyle.setDataList(resultList);

			dataStyle.addDepict("userno", DataTypeConstant.STRING);
			dataStyle.addDepict("loginname", DataTypeConstant.STRING);
			dataStyle.addDepict("username", DataTypeConstant.STRING);
			dataStyle.addDepict("validstart", DataTypeConstant.DATE);
			dataStyle.addDepict("validend", DataTypeConstant.DATE);
			dataStyle.addDepict("inputname", DataTypeConstant.STRING);
			dataStyle.addDepict("id", DataTypeConstant.LONG);
			dataStyle.addDepictByFunction("checkstatus", SysConstant.CheckStatus.class, "getName", new Class[] { long.class });

			dataResult = PangingTools.buildResultPagerInfo(dataStyle);

			if (pangingParam.getReportExportType() == 0) {
				response.getWriter().write(JSONFactory.toJSONString(dataResult, userContext));
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 进入用户详细查看
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/systemmanage/user/toViewUser.web")
	public String toViewUser(HttpServletRequest request, HttpServletResponse response, Model model) {
		try {
			String id = getRequest().getParameter("id");
			UserService userService = (UserService) IssTransactionContainer.getInstance(UserService.class, false);
			User user = userService.findUserById(id);
			model.addAttribute("user", user);
			model.addAttribute("validstart", DataFormat.formatDate(user.getValidstart(), DataFormat.DT_YYYY_MM_DD));
			model.addAttribute("validend", DataFormat.formatDate(user.getValidend(), DataFormat.DT_YYYY_MM_DD));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/WEB-INF/jsp/systemmanage/authoritymanage/userView";
	}

}
